var path=require("path");
var HtmlWebpackPlugin=require("html-webpack-plugin");
var {CleanWebpackPlugin}=require("clean-webpack-plugin");
var  CopyWebpackPlugin=require("copy-webpack-plugin");
module.exports={
    // 设置为开发环境
    mode: "development",
    // 产生source-map 方便查错位置
    devtool:"source-map",
    entry:{
        "js/app":"./src/app.js"
    },
    output:{
        path:path.join(__dirname,"./dist"),
        //这里的name 就是entry中"js/app"
        // [hash:6] 文件名增加6位hash字母，这样每次的文件名就不会重复，避免造成调用缓存
        filename:"[name]-[hash:6].min.js"
    },
    module:{
        rules:[
            { test: /\.art$/, use: { loader: "art-template-loader" } },
            // 引入css，可以使用import 导入css
            { test: /\.css$/i,use: ["style-loader", "css-loader"] }
        ]
    },
    plugins:[
        // 将某个网页作为首页模板，并且将其复制到dist文件夹下，注入压缩打包好js文件
        new HtmlWebpackPlugin({
            template:path.join(__dirname,"./src/index.html"),
            app:"index.html",
            inject:true
        }),
        // 每次导出文件时将以前的js代码删除，避免重复增加
        new CleanWebpackPlugin(),
        // 自动将静态文件内容复制到dist文件夹下
        new CopyWebpackPlugin({
            patterns:[
                {from:path.join(__dirname,"./src/public/img"),to:path.join(__dirname,"./dist/public/img")},
                {from:path.join(__dirname,"./src/public/css"),to:path.join(__dirname,"./dist/public/css")},
                {from:path.join(__dirname,"./src/public/css"),to:path.join(__dirname,"./dist/public/css")},
                {from:path.join(__dirname,"./src/public/font"),to:path.join(__dirname,"./dist/public/font")},
                {from:path.join(__dirname,"./src/public/js"),to:path.join(__dirname,"./dist/public/js")},
                // {from:path.join(__dirname,"./src/public/html"),to:path.join(__dirname,"./dist/public/html")},
            ]
        })
    ],
    devServer:{
        contentBase:path.join(__dirname,"./dist"),
        port:4001,
        proxy:{
            "/api":{
                target:"http://localhost:4010",
                pathRewrite: { '^/api': '' },
            }
        }
    }
}